#include "dat1.h"
#include<stdio.h>
#include<stdlib.h>


status init_list(sqlist *l){
	l->r=(redtype*)malloc(sizeof(redtype)*(list_init_size+1));
	if(!l->r) exit(overflow);
	l->length=0;
	return ok;
}

void destroy_list(sqlist *l){
	if(l->r) free(l->r);
	l->length=0;
}

void clear_list(sqlist *l){
	l->length=0;
}
/*judge whether it is empty*/
status is_empty(sqlist *l){
	if(l->length){
		return false;
	}else{
		return true;
	}
}

int get_length(sqlist *l){
	return l->length;
}

status get_elem(sqlist *l,int i,redtype* e){
	if(i<1||i>l->length) return error;
	*e=l->r[i];
	return ok;
}

int cmp(redtype a,redtype b){
	return a.elem-b.elem;
}
int cmp_elem(elemtype a,elemtype b){
	return a-b;
}

int locate_elem(sqlist *l,elemtype e,func_cmp_elem op){
	for(int i=1;i<=l->length;i++){
		if(op(l->r[i].elem,e)==0){
			return i;
		}
	}
	return error;
}

status prior_elem(sqlist *l,elemtype cur_e,redtype* pre_e){
	int result=locate_elem(l,cur_e,cmp_elem);
	if(!result || result==1){
		return error;
	}
	*pre_e=l->r[result-1];
	return ok;

}

status next_elem(sqlist *l,elemtype cur_e,redtype** next_e){
	int result=locate_elem(l,cur_e,cmp_elem);
	if(!result || result==l->length){
		return error;
	}
	*next_e=&(l->r[result+1]);
	return ok;
}

status list_insert(sqlist *l,int i,elemtype e){
	if(i<1 || l->length>=list_init_size){
		return error;
	}
	if(i>l->length){
		l->r[l->length+1].elem=e;
	}else{
		for(int k=l->length+1;k>i;k--){
			l->r[k]=l->r[k-1];
		}
	}
	l->r[i].elem=e;
	l->length++;
	return ok;
}

status list_delete_elem(sqlist *l,elemtype e){
	int result=locate_elem(l,e,cmp_elem);
	if(!result)return error;
	for(int k=result;k<l->length;k++){
		l->r[k]=l->r[k+1];
	}
	l->length--;
	return ok;

}
status list_delete(sqlist *l,int i){
	if(i<1 || i>l->length)return error;
	for(int k=i;k<l->length;k++){
		l->r[k]=l->r[k+1];
	}
	l->length--;
	return ok;
}

void visited(redtype r){
	printf("%d ",r.elem);
}

void list_traverse(sqlist *l,func_visit op){
	for(int i=1;i<=l->length;i++){
		op(l->r[i]);
	}
}

status print_list_elem(sqlist*l){
	for(int i=1;i<=l->length;i++){
		printf("%d ",l->r[i].elem);
	}
	printf("\n");
}

int main(){
	sqlist l;
	init_list(&l);
	list_insert(&l,1,1);
	list_insert(&l,2,2);
	list_insert(&l,3,3);
	list_insert(&l,4,4);
	list_insert(&l,5,5);
	list_insert(&l,6,6);
	list_insert(&l,7,7);
	list_insert(&l,8,8);
	list_insert(&l,9,9);
	list_insert(&l,10,10);
	list_insert(&l,11,11);
	list_insert(&l,12,12);
	list_insert(&l,13,13);
	list_insert(&l,14,14);
	print_list_elem(&l);
	printf("the length of the list:%d\n",get_length(&l));
	printf("the is_empty of the list:%d\n",is_empty(&l));
	redtype e;
	get_elem(&l,3,&e);
	printf("the get_elem,%d\n",e.elem);
	printf("the locate_elem:%d\n",locate_elem(&l,2,cmp_elem));
	prior_elem(&l,3,&e);
	printf("the elem 3 of prior_elem:%d\n",e.elem);
	redtype *e1;
	next_elem(&l,3,&e1);
	printf("the elem 3 of next_elem:%d\n",e1->elem);
	list_insert(&l,10,100);
	list_insert(&l,1,0);
	print_list_elem(&l);
	list_delete_elem(&l,100);
	list_delete(&l,1);
	list_delete(&l,12);
	list_traverse(&l,visited);
	clear_list(&l);
	print_list_elem(&l);
	destroy_list(&l);
}
